//
//  CGroupMemberMgr.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/25.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CGroupMemberMgr.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"
#import "CGroupMemberModel.h"
@implementation CGroupMemberMgr
//插入单个成员
-(BOOL)insetGroupMember:(CGroupMemberModel *)groupMember
{
    if(groupMember == nil)
    {
        return NO;
    } else {
        if (M_ISSTRNULL(groupMember.GroupID) || M_ISSTRNULL(groupMember.UserId)) {
            return NO;
        }
    }
    __block NSString *sql = nil;
    sql = [NSString stringWithFormat:@"select * from GROUPMEMBERS where userId = \'%@\' AND GroupId = \'%@\'",groupMember.UserId,groupMember.GroupID];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        FMResultSet *result = [db executeQuery:sql];
        if (result && [result next]) {
            ret = YES;
        } else {
            ret = NO;
        }
        [result close];

        if (ret) {
            
            
        } else {
           ret = [db executeUpdate:@"INSERT INTO GROUPMEMBERS(GroupId, UserCode, IsAdmin, LastTime, Available, UserId,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?,?, ?, ?)", M_STOREVALUE(groupMember.GroupID), M_STOREVALUE(groupMember.UserCode),groupMember.IsAdmin?@1:@0,M_STOREVALUE(groupMember.LastTime),groupMember.Available?@1:@0,M_STOREVALUE(groupMember.UserId),M_STOREVALUE(groupMember.yuLiuZiduan1),@(groupMember.yuLiuZiduan2),M_STOREVALUE(groupMember.yuLiuZiduan3)];
        }
    }];
    return ret;
}

- (BOOL)updateGroup:(CGroupMemberModel *)groupMember
{
    if(groupMember == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        //        ret  = [db executeQuery:@"DELETE from ACCOUNT where accountName = %@", account.userName];
        ret = [db executeUpdate:@"UPDATE GROUPMEMBERS SET UserCode = ?, IsAdmin = ?, LastTime = ?, Available = ?, UserId = ? where GroupId = ? ",M_STOREVALUE(groupMember.UserCode),groupMember.IsAdmin?@1:@0,M_STOREVALUE(groupMember.LastTime),groupMember.Available?@1:@0,M_STOREVALUE(groupMember.UserId),M_STOREVALUE(groupMember.GroupID)];
    }];
    return ret;
    
}

//批量插入成员
-(BOOL)batchInsertGroupMember:(NSArray *)groupMemberList
{
    if(groupMemberList == nil || [groupMemberList count] <= 0)
    {
        return NO;
    }
    
    __block NSString *sql = nil;
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CGroupMemberModel *groupMember in groupMemberList)
        {
            if (!M_ISSTRNULL(groupMember.GroupID) && !M_ISSTRNULL(groupMember.UserId)) {
                sql = [NSString stringWithFormat:@"select * from GROUPMEMBERS where userId = \'%@\' AND GroupId = \'%@\'",groupMember.UserId,groupMember.GroupID];
                FMResultSet *result = [db executeQuery:sql];
                if(result && [result next]) {
                    ret = YES;
                } else {
                    ret = NO;
                }
                [result close];
                
                if (ret) {
                    
                    
                } else {
                    ret = [db executeUpdate:@"INSERT INTO GROUPMEMBERS(GroupId, UserCode, IsAdmin, LastTime, Available, UserId,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?,?, ?, ?)", M_STOREVALUE(groupMember.GroupID), M_STOREVALUE(groupMember.UserCode),groupMember.IsAdmin?@1:@0,M_STOREVALUE(groupMember.LastTime),groupMember.Available?@1:@0,M_STOREVALUE(groupMember.UserId),M_STOREVALUE(groupMember.yuLiuZiduan1),@(groupMember.yuLiuZiduan2),M_STOREVALUE(groupMember.yuLiuZiduan3)];
                }
                if(!ret)
                {
                    *rollback = YES;
                    break;
                }

            }
        }

    }];
    return ret;
}
//删除成员
-(BOOL)delGroupMember:(CGroupMemberModel *)groupMember
{
    if(groupMember == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from GROUPMEMBERS where identifer = ?", @(groupMember.GroupMenberIndex)];
    }];
    return ret;
}

//删除成员
-(BOOL)delGroupMemberWithUserId:(NSString *)userId groupId:(NSString *)groupId
{
    if(userId == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from GROUPMEMBERS where UserId = ? AND GroupId = ?", userId,groupId];
    }];
    return ret;
}


- (BOOL)delAllGroupMembe
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from GROUPMEMBERS"];
    }];
    return ret;

}

- (BOOL)delAllGroupMembeWithGroupId:(NSString *)groupId
{
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from GROUPMEMBERS where GroupId = ?", groupId];
    }];
    return ret;

}

-(CGroupMemberModel *)loadGroupMemberByUserID:(NSString *)userId groupId:(NSString *)groupId
{
    if (M_ISSTRNULL(userId)) {
        return nil;
    }
    NSString *sql = nil;
    __block CGroupMemberModel *groupMember = nil;
    sql = [NSString stringWithFormat:@"select * from GROUPMEMBERS where userId = \'%@\' AND GroupId = \'%@\'",userId,groupId];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            groupMember = [[CGroupMemberModel alloc] init];
            groupMember.GroupID = [result stringForColumn:@"GroupId"];
            groupMember.UserCode = [result stringForColumn:@"UserCode"];
            groupMember.IsAdmin = [result boolForColumn:@"IsAdmin"];
            groupMember.GroupMenberIndex = [result longLongIntForColumn:@"identifer"];
            groupMember.Available = [result boolForColumn:@"Available"];
            groupMember.UserId = [result stringForColumn:@"UserId"];
            groupMember.LastTime = [result stringForColumn:@"LastTime"];
        }
        [result close];
    }];
    return groupMember;
}

//加载成员
-(NSArray *)loadAllGroupMemberByGroupID:(NSString *)groupID
{
    if (M_ISSTRNULL(groupID)) {
        return nil;
    }
    NSString *sql = nil;
    __block NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
    sql = [NSString stringWithFormat:@"select * from GROUPMEMBERS where GroupId = \'%@\'",groupID];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CGroupMemberModel *groupMember = [[CGroupMemberModel alloc] init];
            groupMember.GroupID = [result stringForColumn:@"GroupId"];
            groupMember.UserCode = [result stringForColumn:@"UserCode"];
            groupMember.IsAdmin = [result boolForColumn:@"IsAdmin"];
            groupMember.GroupMenberIndex = [result longLongIntForColumn:@"identifer"];
            groupMember.Available = [result boolForColumn:@"Available"];
            groupMember.UserId = [result stringForColumn:@"UserId"];
            groupMember.LastTime = [result stringForColumn:@"LastTime"];
            [array addObject:groupMember];
            groupMember = nil;
        }
        [result close];
    }];
    return array;
}



@end
